//
//  UIViewController+Tips.swift
//  ifanr
//
//  Created by 😱😱 on 16/9/13.
//  Copyright © 2016年 😱😱. All rights reserved.
//

import UIKit

extension UIViewController {
    /**
     在屏幕中间出现一段提示文字
     
     - parameter tips:     提示文字
     - parameter duration: 持续时间
     */
    //TODO: 完善:根据当前背景，自动选择背景、字体的颜色
    public func showTips(tips:String, duration: NSTimeInterval){
        // 无意义的tips
        if duration == 0 || tips.isEmpty {
            return
        }
        
        var tipsLabel = self.view.viewWithTag(1994) as? UILabel
        if tipsLabel == nil {
            tipsLabel = UILabel(frame: CGRectMake(0, 0, self.view.frame.size.width - 50, self.view.frame.size.height - 150))
        }
        
        tipsLabel!.frame = CGRectMake(0, 0, self.view.frame.size.width - 50, self.view.frame.size.height - 150)
        tipsLabel!.tag = 1994
        tipsLabel!.alpha = 0
        tipsLabel!.text = tips
        tipsLabel!.backgroundColor = UIColor.clearColor()
        tipsLabel!.font = UIFont.systemFontOfSize(15)
        tipsLabel!.textColor = UIColor.whiteColor()
        tipsLabel!.numberOfLines = 0
        tipsLabel!.sizeToFit()
        var showsFrame = CGRect.zero
        showsFrame = CGRectMake((CGRectGetWidth(self.view.frame) - CGRectGetWidth(tipsLabel!.frame)) / 2.0 - 20, (CGRectGetHeight(self.view.frame) - CGRectGetHeight(tipsLabel!.frame)) / 2.0 - 15, CGRectGetWidth(tipsLabel!.frame) + 40, CGRectGetHeight(tipsLabel!.frame) + 30)
        
        var backgroundView = self.view.viewWithTag(1995)
        if backgroundView == nil{
            backgroundView = UIView(frame: showsFrame)
        }
        backgroundView!.frame = showsFrame
        backgroundView!.tag = 1995
        backgroundView!.backgroundColor = UIColor.blackColor()
        backgroundView!.alpha = 0
        backgroundView!.layer.cornerRadius = 5
        backgroundView!.clipsToBounds = true
        self.view.addSubview(backgroundView!)
        tipsLabel!.center = backgroundView!.center
        self.view.addSubview(tipsLabel!)
        UIView.animateWithDuration(0.25, animations: {() -> Void in
            tipsLabel!.alpha = 1
            backgroundView!.alpha = 0.5
        })
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, Int64(duration * Double(NSEC_PER_SEC))), dispatch_get_main_queue(), {() -> Void in
            UIView.animateWithDuration(0.25, animations: {() -> Void in
                tipsLabel!.alpha = 0
                backgroundView!.alpha = 0
                }, completion: {(finished: Bool) -> Void in
                    tipsLabel!.removeFromSuperview()
                    backgroundView!.removeFromSuperview()})})
    }
    
    func showProgress () {
        let progressView = UIActivityIndicatorView()
        progressView.activityIndicatorViewStyle = .Gray
        progressView.hidesWhenStopped = true
        progressView.tag = 500
        self.view.addSubview(progressView)
        
        progressView.snp_makeConstraints { (make) in
            make.center.equalTo(self.view)
            make.height.width.equalTo(20)
        }
        
        progressView.startAnimating()
    }
    
    func hiddenProgress() {
        for view in self.view.subviews {
            if view.tag == 500 {
                let indicatorView : UIActivityIndicatorView = view as! UIActivityIndicatorView
                indicatorView.stopAnimating()
                indicatorView.removeFromSuperview()
            }
        }
    }
}
